package com.ch.clinking.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.ch.clinking.R;
import com.ch.clinking.databinding.ActivityCalculateProfitBinding;

import java.text.DecimalFormat;

public class CalculateProfitActivity extends AppCompatActivity {

    private ActivityCalculateProfitBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityCalculateProfitBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.calculateProfitCalculateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String salePriceString = binding.calculateProfitSalePrice.getText().toString().trim();
                String costString = binding.calculateProfitSaleCost.getText().toString().trim();
                String totalPercentString = binding.calculateProfitSaleTotalPercent.getText().toString().trim();
                String sizePercentString = binding.calculateProfitSaleSizePercent.getText().toString().trim();
                String qualityScoreString = binding.calculateProfitQualityScore.getText().toString().trim();
                String multipleString = binding.calculateProfitMultiple.getText().toString().trim();

                if (salePriceString.isEmpty() || costString.isEmpty() || totalPercentString.isEmpty() || sizePercentString.isEmpty()) {

                    Toast.makeText(CalculateProfitActivity.this, "必填项未填写完成", Toast.LENGTH_SHORT).show();
                    return;

                } else if (qualityScoreString.isEmpty() && multipleString.isEmpty()) {

                    Toast.makeText(CalculateProfitActivity.this, "品质分和罚款倍率不可同时为空", Toast.LENGTH_SHORT).show();
                    return;

                }


                double salePrice = Double.valueOf(salePriceString);
                double cost = Double.valueOf(costString);
                double totalPercent = Double.valueOf(totalPercentString);
                double sizePercent = Double.valueOf(sizePercentString);
                double qualityScore = qualityScoreString.isEmpty() ? 0 : Double.valueOf(qualityScoreString);
                double multiple = multipleString.isEmpty() ? 0 : Double.valueOf(multipleString);

                if (!qualityScoreString.isEmpty()) {
                    if (qualityScore > 0 && qualityScore < 60) {
                        multiple = 5;
                    } else if (qualityScore >= 60 && qualityScore < 70) {
                        multiple = 2.5;
                    } else if (qualityScore >= 70 && qualityScore < 90) {
                        multiple = 1.5;
                    } else if (qualityScore >= 90 && qualityScore <= 100) {
                        multiple = 0;
                    }

                    binding.calculateProfitMultiple.setText(String.valueOf(multiple));

                }


                // 罚款
                double deduct = (totalPercent - sizePercent) * multiple * salePrice + cost;

                // 成功卖出的利润
                double profitSale = (100 - totalPercent) * (salePrice - cost);

                double profit = (profitSale - deduct)/100.0;

                // 创建 DecimalFormat 实例，设置保留 3 位小数
                DecimalFormat df = new DecimalFormat("#.###");

                // 格式化结果
                String formattedResult = df.format(profit);

                binding.calculateProfitProfit.setText(formattedResult);

            }
        });


    }
}